/**
 *问题评价
 */

define(["jquery", "datagridServ","sysmanMain", "bizProto","btnServ","msgServ","flowInit","buttonEvent"],
		function($,datagridServ, sysmanMain, bizProto,btnServ,msgServ,flowInit,buttonEvent) {
		'use strict';
		var rootid = 'panel-satisfaction-evaluate-subject';
		var editIndex = -1;
		var window_id = 'satisfaction-evaluate-subject-win';
		var _bizProto;
		var activityListGrid;
		var _buttonConfig;

		var public_method ={	
			
			init:function(doc){
				private_method.init(doc);
			}
	
			
		}
		
		var private_method ={

			
			newsave:function(){

				 $(activityListGrid).datagrid('acceptChanges');
				 var _data = $(activityListGrid).datagrid('getData')
				 var _patientId = window.location.search;
				 console.info(_patientId);
				 for (var i = 0; i < _data.total; i++)
				 {
				 	_data.rows[i].patient_code = _patientId.substring(4, _patientId.length);
				 	console.info(_data.rows[i]);
				 }
				 	if(_data && _data.rows){
				// 		//save data to remote
						var _url = 
							 '/honest/satisfactionEvaluateResult/saveInpatient.action';
				//		var _url = _bizProto.rootPath + '/flowTaskConfig/save.action'
						$.ajax({
				 			url:_url,
				 			type : 'post',
				 			dataType:'json',
				 			data:$.toJSON(_data.rows),
				 			contentType:'appliction/json;charset=utf-8',						
				 			success:function(data){
				 				msgServ.Msgslide('操作成功');
				 			}
				 		})
				 	}		
			},

			//初始化页面元素
			init:function(doc){	
				_bizProto = new bizProto({doc:doc,rootId:rootid,
									  panelSelector:'div.well',
									  subSystemCode:'satisfaction',
									  moduleCode:'satisfactionEvaluateSubject'});
				_bizProto.gridHeight += 35;
				_buttonConfig={
						 "保存":private_method.newsave			 							
					};
				var qryQuestionUrl =  '/honest/satisfactionEvaluateSubject/query/getInpatientQueListDomain.action';
				var activityList = $('#'+ rootid +' table.dg-list');
					    activityListGrid =	activityList.datagrid({
						url : qryQuestionUrl,
						height : 600,
						onClickCell:private_method.editCell,
						columns:[[	       
							{field:'problem_code',title:'问题编号',halign:'center',width:60},
							{field:'problem_subject',title:'问题内容',halign:'center',width:100}, 
							{field:'evaluate_type',title:'问题类型',halign:'center',width:60},     
							{field:'score',title:'问题分数',width:60,align:'right',halign:'center',editor:'text'}
						]],
						rowStyler:function(index,row){   
					        if (row.state){
				        	 	if (row.state<0){
				        			return 'background-color:pink;';				        			
					        	}		        			 
					        }   
					    },
					    rownumbers:true,
						animate: true,										
						striped:true,
						singleSelect:true,
						border:true,					
						idField: 'id',										
						fitColumns: true
					});
					
					private_method.bindBtnEvent();
					$.messager.progress('close');
		
				
		},

			//绑定按钮事件
			bindBtnEvent:function(){
					
				var window_btn = $(_bizProto.pageDoc).find('a.function-btn')
					$(window_btn).unbind('click')
					$(window_btn).bind('click',	private_method.function_btn_listenner)
					
				},
				//功能按钮监听函数
				function_btn_listenner:function(e){
					
					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})
					
				},


					

			editCell:function(index,field,value){
					
					if(window.console)
						console.info(' editCell fired ...')
					if(field=='score' ){
						if(editIndex>-1 && (editIndex!=index)){
							$(activityListGrid).datagrid('acceptChanges');
							$(activityListGrid).datagrid('endEdit',editIndex);							
						}
						
						$(activityListGrid).datagrid('beginEdit',index);
							editIndex = index
						
					}
				}
		}


			
		
		return public_method;
			
})